//
//  UICollectionView+Refresh.swift
//  ContactPoster
//
//  Created by TSYH on 2023/11/22.
//

import Foundation
import UIKit
import MJRefresh

public extension UIScrollView {
    func setRefresh(handler: (() -> Void)?) {
        let header = MJRefreshNormalHeader {
            handler?()
        }
        header.lastUpdatedTimeLabel?.isHidden = true
        header.stateLabel?.isHidden = true
        header.setTitle("", for: .idle)
//        header.arrowView?.image = header.arrowView?.image?.withTintColor(.white, renderingMode: .alwaysTemplate)
//        header.arrowView?.tintColor = .white
        header.loadingView?.color = .white
        mj_header = header
    }
    
    func setLoadMore(text: String = "No more", handler: (() -> Void)?) {
        let footer = MJRefreshAutoNormalFooter {
            handler?()
        }
        footer.isRefreshingTitleHidden = true
        footer.setTitle("", for: .idle)
        footer.setTitle(text, for: .noMoreData)
        footer.loadingView?.color = .white
        footer.stateLabel?.textColor = .white.withAlphaComponent(0.4)
        mj_footer = footer
    }
    
    @objc open func endRefreshing(noMore: Bool = false) {
        mj_header?.endRefreshing()
        if noMore {
            mj_footer?.endRefreshingWithNoMoreData()
        } else {
            mj_footer?.endRefreshing()
        }
    }
}
